import { Inject, Service } from 'typedi';

export enum codeList {
    Success = 200,
    Error = 500,
    NoFound = 404
}

export enum msgList {
    Success = "请求成功",
    Error = "请求成功",
    NoFound = "未找到用户",
    SystemError = "系统错误",
    DeleteSuccess = "删除成功",
    NoFoundToken = "缺少token",
    DecodedTokenError = "解密token失败",
    PasswordError = "用户密码错误，请检查",
    UserNameExist = "用户名已存在，请改名",
    addUserSuccess = "注册成功",
    pushMessageSuccess = "发布成功",
}

export interface res<T> {
    code: number;
    msg: string;
    data: T;
}

@Service()
export class JsonResult<T> {
    // 状态码
    private _code!: number;
   
    // 错误的中文信息
    private _msg!: string;
   
    // 接口数据
    private _data!: T;
   
    public get code(): number {
        return this._code;
    }
    public set code(value: number) {
        this._code = value;
    }

    public get msg(): string {
        return this._msg;
    }
    public set msg(value: string) {
        this._msg = value;
    }
    public get data(): T {
        return this._data;
    }
    public set data(value: T) {
        this._data = value;
    }

    success<U extends T>(data: U, msg?: string, code?: number ) {
        this.code = code || codeList.Success;
        this.msg = msg || msgList.Success;
        this.data = data;
        return {
            code: this.code,
            msg: this.msg,
            data: this.data,
        }
    }


    error<U extends T>(data: U, msg?: string, code?: number) {
        this.code = code || codeList.Error;
        this.msg = msg || msgList.SystemError;
        this.data = data;
        return {
            code: this.code,
            msg: this.msg,
            data: this.data,
        }
    }
}
